﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using TuKu.Common;

namespace TuKu.Mop
{
    public partial class mopPage : Form
    {
        public mopPage()
        {
            InitializeComponent();
        }

        private void btnDo_Click(object sender, EventArgs e)
        {
            WebClient wClient = new WebClient();
            String pageData = Encoding.Default.GetString(wClient.DownloadData(txtmopurl.Text));
            txtResult.Text = pageData;

        }

        private void btnGetPager_Click(object sender, EventArgs e)
        {
            string patten1 = @"<\!--分页开始-->([\S\s]*?)<\!--分页结束-->";
            string patten2 = @"<p\s*class=""textcen"">\s*<a([\S\s]*?)</a>\s*</p>";
            string patten3 = @"src=""([\S\s]*?)""";

            Match pagedata = Regex.Match(txtResult.Text, patten1, RegexOptions.IgnoreCase);
            Match pageImgNode = Regex.Match(txtResult.Text, patten2, RegexOptions.IgnoreCase);
            if (pagedata.Success && pageImgNode.Success)
            {
                txtPager.Text = pagedata.Value;
                txtImgNode.Text = Regex.Match(pageImgNode.Value, patten3, RegexOptions.IgnoreCase).Result("$1");
            }
        }

        private void btnPagerUrl_Click(object sender, EventArgs e)
        {
            txtPagerUrl.Text = "";
            string patten1 = @"((http://pic))+(((/?)+[\w-.]+(/))*)+[\w-./]+\.+(shtml)";

            StringBuilder sb = new StringBuilder();
            Regex re = new Regex(patten1, RegexOptions.IgnoreCase);
            MatchCollection mc = re.Matches(txtPager.Text);
            for (int i = 0; i < mc.Count - 1; i++)
            {
                sb.Append(mc[i].Value + "\r\n");
            }
            txtPagerUrl.Text = sb.ToString();
        }

        private void mopPage_Load(object sender, EventArgs e)
        {
            string strsql = "select top 10 * from moptable ";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            txtmopurl.DataSource = dt;
            txtmopurl.DisplayMember = "url";
            txtmopurl.ValueMember = "id";
        }

        private void btnGetPageCount_Click(object sender, EventArgs e)
        {
            string patten1 = @"<ul\sclass=""bf"">([\S\s]*?)</ul>"; //分页开始，结束
            string patten4 = @"<li\sclass=""bfpic"">([\S\s]*?)</li>"; //分页LI

            string patten2 =@"<div\sclass=""content"">([\S\s]*?)</div>";//当前页图片节点
            string patten3 =@"src=""([\S\s]*?)""";//图片地址
            string txtImgNode = string.Empty;
            string strsql = "select * from moptable where picpage is null";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            WebClient wClient = new WebClient();
            String pageData = string.Empty;
            String txtPager = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                pageData = Encoding.Default.GetString(wClient.DownloadData(dr["url"].ToString()));
                Match pagedata = Regex.Match(pageData, patten1, RegexOptions.IgnoreCase);

                Match currentImg = Regex.Match(pageData, patten2, RegexOptions.IgnoreCase);

                if (pagedata.Success)
                {
                    txtPager = pagedata.Value;
                    Regex re = new Regex(patten4, RegexOptions.IgnoreCase);
                    txtImgNode = Regex.Match(currentImg.Value, patten3, RegexOptions.IgnoreCase).Result("$1");
                    MatchCollection mc = re.Matches(txtPager);
                    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql("update moptable set pagecount=" + (mc.Count+1).ToString() + " , picpage='"+txtImgNode+"' where id=" + dr["id"].ToString());
                }

            }
            MessageBox.Show("完成");


        }

        private void btnGetpicPage_Click(object sender, EventArgs e)
        {

            string patten1 = @"<\!--分页开始-->([\S\s]*?)<\!--分页结束-->";
            string patten2 = @"<p\s*class=""textcen"">\s*<a([\S\s]*?)</a>\s*</p>";
            string patten3 = @"src=""([\S\s]*?)""";
            WebClient wClient = new WebClient();
            string txtImgNode = String.Empty;
            string strsql = "select * from moptable where cateid<>3 and picpage is null";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                string page = Encoding.Default.GetString(wClient.DownloadData(dr["url"].ToString()));
                Match pageImgNode = Regex.Match(page, patten2, RegexOptions.IgnoreCase);
                if (pageImgNode.Success)
                {
                    txtImgNode = Regex.Match(pageImgNode.Value, patten3, RegexOptions.IgnoreCase).Result("$1");
                    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql("update moptable set picpage='" + txtImgNode + "' where id=" + dr["id"].ToString());

                }
            }
            MessageBox.Show("完成");
        }

        private void btnsubtable_Click(object sender, EventArgs e)
        {
            //添加流程 1.加入urltable
            string strsql = "select * from moptable";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            StringBuilder sb = new StringBuilder();
            int url = 78948;
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 2; i <= int.Parse(dr["pagecount"].ToString()); i++)
                {
                    sb.Append("insert into mopsubtable (url,title,currentpage,picPage,categoryid,addtime) values ( ");
                    sb.Append("'" + url + "',");
                    sb.Append("'" + dr["alt"].ToString() + "',");
                    sb.Append(" "+i.ToString()+", ");
                    sb.Append("'"+dr["url"].ToString()+"_" + i.ToString() + "#n', ");
                    sb.Append(" " + dr["cateid"].ToString() + ", ");
                    sb.Append(" '" +DateTime.Now.ToString() + "' ");
                    sb.Append(" ) ");
                    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sb.ToString());
                    sb.Remove(0, sb.Length);
                }
                url++;

            }

            //getSubImgurl();
            MessageBox.Show("完成");
        }

        private void getSubImgurl()
        {
            string patten1 = @"<\!--分页开始-->([\S\s]*?)<\!--分页结束-->";
            string patten2 = @"<p\s*class=""textcen"">\s*<a([\S\s]*?)</a>\s*</p>";
            string patten3 = @"src=""([\S\s]*?)""";
            string pageData = string.Empty;
            string strsql = "select * from mopsubtable";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            StringBuilder sb = new StringBuilder();
            WebClient wClient = new WebClient();
            string txtImgNode = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                pageData = Encoding.Default.GetString(wClient.DownloadData(dr["picPage"].ToString()));
                Match pageImgNode = Regex.Match(pageData, patten2, RegexOptions.IgnoreCase);
                if (pageImgNode.Success)
                {
                    txtImgNode = Regex.Match(pageImgNode.Value, patten3, RegexOptions.IgnoreCase).Result("$1");
                    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql("update mopsubtable set img='"+txtImgNode+"' where id="+dr["id"].ToString());
                }

            }
        }

        private void btnAddTourltable_Click(object sender, EventArgs e)
        {
            string strsql = "select * from moptable ";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            int i = 78948;
            
            StringBuilder sb = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                sb.Append("insert into urltable (url,pagenumber,title,finish,categoryName,picPage,categoryid) values ( ");
                sb.Append(" 'http://tu.pps.tv/pics/"+i.ToString()+"/index.html', ");
                sb.Append(" "+dr["pagecount"].ToString()+", ");
                sb.Append(" '"+dr["alt"].ToString().Replace("'","")+"', ");
                sb.Append(" 0,");
                sb.Append(" '"+i.ToString()+"',");
                sb.Append(" '" + "http://www.tujuduo.com/upload/2008-10/" + FileManager.GetFileNameFromUrl(dr["picPage"].ToString()) + "',");
                sb.Append(" "+dr["cateid"].ToString()+" ");
                sb.Append(" )");
                Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sb.ToString());
                sb.Remove(0, sb.Length);
                sb.Append("insert into urllist (url,alt,img,orderid,cateid) values (");
                sb.Append(" 'http://tu.pps.tv/pics/" + i.ToString() + "/index.html', ");
                sb.Append(" '" + dr["alt"].ToString().Replace("'", "") + "',");
                sb.Append(" '" + "http://www.tujuduo.com/upload/2008-10/" + FileManager.GetFileNameFromUrl(dr["img"].ToString()) + "',");
                sb.Append(" 1,");
                sb.Append(" "+dr["cateid"].ToString()+" ");
                sb.Append(" ) ");
                Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql(sb.ToString());
                sb.Remove(0, sb.Length);
                i++;
            }
            MessageBox.Show("完成");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //http://pic.kuaiche.com/content/1829608_3#n
            //http://image.mop.com/bbspic/tt/2008/0429/30-22-7483-22-294-283-20080429100953.jpg

            string patten2 = @"<div\sclass=""content"">([\S\s]*?)</div>";//当前页图片节点
            string patten3 = @"src=""([\S\s]*?)""";//图片地址
            string txtImgNode = string.Empty;
            string strsql = "SELECT id,picPage FROM mopsubtable WHERE img ='NOPIC' ";
            DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            WebClient wClient = new WebClient();
            String pageData = string.Empty;
            String txtPager = string.Empty;
            foreach (DataRow dr in dt.Rows)
            {
                pageData = Encoding.Default.GetString(wClient.DownloadData(dr["picPage"].ToString()));
                Match currentImg = Regex.Match(pageData, patten2, RegexOptions.IgnoreCase);
                if (currentImg.Success)
                {
                    txtImgNode = Regex.Match(currentImg.Value, patten3, RegexOptions.IgnoreCase).Result("$1");
                    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql("update mopsubtable set img='" + txtImgNode + "' where id=" + dr["id"].ToString());
                }

            }
            MessageBox.Show("完成");
            //string strsql = "select id,picPage from mopsubtable";
            //DataTable dt = Maticsoft.DBUtility.DbHelperOleDb.Query(strsql).Tables[0];
            //foreach (DataRow dr in dt.Rows)
            //{
            //    Maticsoft.DBUtility.DbHelperOleDb.ExecuteSql("update mopsubtable set picPage='" + "http://www.tujuduo.com/upload/2008-12/" + dr["picPage"].ToString().Substring(41) + "' where id="+dr["id"].ToString());
            //}
            //MessageBox.Show("ok");

        }
    }
}
